package es.mgil.linternas;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

public class LinternaSuper extends Activity {
	private ImageButton boton;
	private RelativeLayout fondo;
	private int colorBlanco = 0xffffffff;
	private int colorNegro = 0xff000000;
	private int colorAmarillo = 0xffffff00;
	private int colorAzul = 0xff0000ff;
	private int colorRojo = 0xffff0000;
	private int defaultColor = colorBlanco;
	private boolean encendida = false;

	private SharedPreferences preferences;
	private Editor editor;
	private Menu menu;

	private final String selectedColor = "selectedColor";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.linterna_super);

		boton = (ImageButton) findViewById(R.id.boton_encender);
		fondo = (RelativeLayout) findViewById(R.id.fondo);
		fondo.setBackgroundColor(colorNegro);

		preferences = getPreferences(MODE_PRIVATE);
		editor = preferences.edit();

		defaultColor = preferences.getInt(selectedColor, colorBlanco);
	}

	public void encender(View view) {
		if (encendida) {
			// apagar
			encendida = false;
			fondo.setBackgroundColor(colorNegro);
			boton.setImageResource(R.drawable.botonon);
			boton.setScaleType(ScaleType.valueOf("FIT_CENTER"));
		} else {
			// encender
			encendida = true;
			changeColor();
			boton.setImageResource(R.drawable.botonoff);
			boton.setScaleType(ScaleType.valueOf("FIT_CENTER"));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.menu = menu;
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_super_linterna, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		uncheckAllItems();

		switch (item.getItemId()) {
		case R.id.amarillo:
			defaultColor = colorAmarillo;
			changeColor();
			item.setChecked(true);
			return true;
		case R.id.blanco:
			defaultColor = colorBlanco;
			changeColor();
			item.setChecked(true);
			return true;
		case R.id.rojo:
			defaultColor = colorRojo;
			changeColor();
			item.setChecked(true);
			return true;
		case R.id.azul:
			defaultColor = colorAzul;
			changeColor();
			item.setChecked(true);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void uncheckAllItems() {
		for (int i = 0; i < menu.size(); i++) {
			menu.getItem(i).setChecked(false);
		}
	}

	private void changeColor() {
		if (encendida) {
			fondo.setBackgroundColor(defaultColor);
			editor.putInt(selectedColor, defaultColor);
			editor.commit();
		}
	}
}
